package me.khrystal.rxnews.module.photo.presenter;

import com.socks.library.KLog;

import java.util.List;

import me.khrystal.rxnews.base.BasePresenterImpl;
import me.khrystal.rxnews.bean.SinaPhotoList;
import me.khrystal.rxnews.common.DataLoadType;
import me.khrystal.rxnews.module.photo.model.IPhotoListInteractor;
import me.khrystal.rxnews.module.photo.model.IPhotoListInteractorImpl;
import me.khrystal.rxnews.module.photo.presenter.IPhotoListPresenter;
import me.khrystal.rxnews.module.photo.view.IPhotoListView;

/**
 * usage:
 * author: kHRYSTAL
 * create time: 16/4/29
 * update time:
 * email: 723526676@qq.com
 */
public class IPhotoListPresenterImpl
        extends BasePresenterImpl<IPhotoListView,List<SinaPhotoList.DataEntity.PhotoListEntity>>
        implements IPhotoListPresenter {

    private IPhotoListInteractor<List<SinaPhotoList.DataEntity.PhotoListEntity>> mPhotoListInteractor;
    private String mPhotoId;
    private int mStartPage;

    private boolean mIsRefresh = true;
    private boolean mHasInit;

    public IPhotoListPresenterImpl(IPhotoListView view,String postId,int startPage) {
        super(view);
        mPhotoId = postId;
        mStartPage = startPage;
        mPhotoListInteractor = new IPhotoListInteractorImpl();
        mSubscription = mPhotoListInteractor.requestPhotoList(this,postId,startPage);
    }

    @Override
    public void beforeRequest() {
        if (!mHasInit){
            mView.showProgress();
        }
    }

    @Override
    public void requestError(String msg) {
        super.requestError(msg);
        mView.updatePhotoList(null,
                mIsRefresh ? DataLoadType.TYPE_REFRESH_FAIL : DataLoadType.TYPE_LOAD_MORE_FAIL);
    }

    @Override
    public void refreshData() {
        mStartPage = 1;
        mIsRefresh = true;
        mSubscription = mPhotoListInteractor.requestPhotoList(this, mPhotoId, mStartPage);
    }

    @Override
    public void loadMoreData() {
        KLog.e("加载更多数据: " + mPhotoId + ";" + mStartPage);
        mIsRefresh = false;
        mSubscription = mPhotoListInteractor.requestPhotoList(this, mPhotoId, mStartPage);
    }

    @Override
    public void requestSuccess(List<SinaPhotoList.DataEntity.PhotoListEntity> data) {
        KLog.e("请求成功: ");
        mHasInit = true;
        if (data != null && data.size() > 0) {
            mStartPage++;
        }
        mView.updatePhotoList(data,
                mIsRefresh ? DataLoadType.TYPE_REFRESH_SUCCESS : DataLoadType.TYPE_LOAD_MORE_SUCCESS);

    }
}
